package case1;

class SingletonLazy{
//    //线程不安全
//    private static SingletonLazy instance = null;
//    public static SingletonLazy getInstance(){
//        if(instance == null){
//            instance = new SingletonLazy();
//        }
//        return instance;
//    }
//    private SingletonLazy(){}

    //线程安全
    private static volatile SingletonLazy instance = null;
    public static SingletonLazy getInstance(){
        //判断是否加锁
        if(instance == null){
            synchronized (SingletonLazy.class){
                if(instance == null){
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }
    private SingletonLazy(){}


}

public class Demo2 {
    public static void main(String[] args) {
        SingletonLazy s1 = SingletonLazy.getInstance();
        SingletonLazy s2 = SingletonLazy.getInstance();
        System.out.println(s1 == s2);
    }
}
